package team.kirohuji.utils;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.crypto.RandomNumberGenerator;
import org.apache.shiro.crypto.SecureRandomNumberGenerator;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.util.ByteSource;

/**
 * Shiro工具类
 * 
 * @author kirohuji
 *
 */
public class ShiroUtils {

	/**
	 * 获取shiro 身份信息
	 * 
	 * @return Subject
	 */
	public static Subject getSubject() {
		return SecurityUtils.getSubject();
	}

	/**
	 * shiro 退出登录
	 * 
	 */
	public static void logout() {

		SecurityUtils.getSubject().logout();
	}

	/**
	 * shiro 提供加密算法
	 * 
	 * @param txt
	 *            明文
	 * @param salt
	 *            干扰字符串，可以为null
	 * @return String
	 */
	public static String MD5(String txt, String salt) {

		return new SimpleHash("md5", txt, ByteSource.Util.bytes(salt), 252).toHex();
	}

	public static String salt() {
		RandomNumberGenerator randomNumberGenerator = new SecureRandomNumberGenerator();
		return randomNumberGenerator.nextBytes().toHex();
	}

	public static void main(String[] args) {
		RandomNumberGenerator randomNumberGenerator = new SecureRandomNumberGenerator();
		String dd = randomNumberGenerator.nextBytes().toHex();
		System.out.println(dd);
		System.out.println(MD5("zyd1362848650", dd));
	}
}